package com.rong.dao.impl;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Component;

import com.rong.bean.UserBean;
import com.rong.dao.IUserDao;
import com.rong.mybatis.DBAccess;

@Component
public class UserDao implements IUserDao {

	public boolean userRegister(UserBean user) {

		DBAccess dbAccess = new DBAccess();
		SqlSession sqlSession = null;
		UserBean userBean = null;
		try {
			sqlSession = dbAccess.getSqlSession();
			// 检查用户名是否存在
			userBean = sqlSession.selectOne("UserManager.userCheck", user);
			if (userBean != null) {
				return false;
			}

			int num = sqlSession.insert("UserManager.userRegister", user);
			if (num == 1) {
				sqlSession.commit();
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (sqlSession != null) {
				sqlSession.close();
			}
		}
		return false;
	}

	public UserBean userLogin(UserBean user) {
		DBAccess dbAccess = new DBAccess();
		SqlSession sqlSession = null;
		UserBean userBean = null;
		try {
			sqlSession = dbAccess.getSqlSession();
			userBean = sqlSession.selectOne("UserManager.userLogin", user);
			return userBean;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (sqlSession != null) {
				sqlSession.close();
			}
		}
		return null;
	}
}
